<script>
import UpdateTree from "./update-tree.vue";
import Plantecharts from "../../main/plantecharts.vue";
export default {
  name: "AdminTreePlan",
  props: {
    tree: {
      type: Object,
      default: [],
    },
  },
  data() {
    return {
      index:null,
      detail: [
        {
          list: [],
          total: 0,
          pageNum: 0,
          pageSize: 0,
        },
      ],
      current: {},
      dialogFormVisible: false,
      user: {},
      dialogFormVisible2: false,
      list2 : []
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(this.detail.list);
      this.current = row;
      this.dialogFormVisible = true;
    },
    confirmEvent() {
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin//projecttwo/delete",
        data: this.$qs.stringify(this.detail.list[this.index]),
      }).then((res) => {
        if (res.data.code == 1) {
          ElMessage(res.data.msg);
        } else {
          console.log(res.data.msg);
        }
      });
    },
    handleDelete(index){this.index = index},
    handleSizeChange(size) {
      this.detail.pageSize = size;
      this.mount();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.mount();
    },
    mount() {
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin/projecttwo",
        data:
          this.$qs.stringify(this.user) +
          "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        // console.log(res.data);
        this.project2 = res.data;
        if (res.data.code == 1) {
          this.detail.list = res.data.data.list;
          this.detail.pageSize = res.data.data.pageSize;
          this.detail.pageNum = res.data.data.pageNum;
          this.detail.total = res.data.data.total;
        } else {
          console.log("error");
        }
      });
    },
    char1(){
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin/projecttwo",
        data:this.$qs.stringify({
          community: 1
        }) + "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        console.log(res.data);
        this.project2 = res.data;
        if (res.data.code == 1) {
          this.list2 = res.data.data.list;
          this.dialogFormVisible2 = true;
        } else {
          console.log("error");
        }
      });
      
    },
    char2(){
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin/projecttwo",
        data:this.$qs.stringify({
          community: 2
        }) + "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        if (res.data.code == 1) {
          this.list2 = res.data.data.list;
          this.dialogFormVisible2 = true;
        } else {
          console.log("error");
        }
      });
    },
    char3(){
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin/projecttwo",
        data:this.$qs.stringify({
          community: 3
        }) + "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        if (res.data.code == 1) {
          this.list2 = res.data.data.list;
          this.dialogFormVisible2 = true;
        } else {
          console.log("error");
        }
      });
    },
    char4(){
      this.$axios({
        method: "post",
        url: "http://localhost:8080//nav/admin/projecttwo",
        data:this.$qs.stringify({
          community: 4
        }) + "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        if (res.data.code == 1) {
          this.list2 = res.data.data.list;
          this.dialogFormVisible2 = true;
        } else {
          console.log("error");
        }
      });
    },
  },
  mounted() {
    this.user = this.$store.state.user;
    this.mount();
  },
  components: { UpdateTree, Plantecharts },
};
</script>

<template>
  <el-button type="primary" plain @click="char1">查看一社区图表</el-button>
  <el-button type="primary" plain @click="char2">查看二社区图表</el-button>
  <el-button type="primary" plain @click="char3">查看三社区图表</el-button>
  <el-button type="primary" plain @click="char4">查看四社区图表</el-button>
  <el-table
    ref="tree"
    :data="detail.list"
    highlight-current-row
    style="width: 100%"
    border
    @current-change="handleCurrentChange"
  >
    <el-table-column type="index" label="序号"></el-table-column>
    <el-table-column prop="id" v-if="false"></el-table-column>
    <el-table-column prop="name" label="树名"></el-table-column>
    <el-table-column
      prop="plantCommunityName"
      label="所属社区"
    ></el-table-column>
    <el-table-column prop="number" label="总量"></el-table-column>
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index,scope.row)">
          修改
        </el-button>
        <el-dialog v-model="dialogFormVisible" title="修改信息">
          <UpdateTree :current="current"></UpdateTree>
        </el-dialog>
        <el-popconfirm
          title="你确定要删除该选项吗？"
          @confirm="confirmEvent"
          @cancel="cancelEvent"
        >
          <template #reference>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index)"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    v-if="this.detail.total > 5"
    v-model:currentPage="detail.pageNum"
    v-model:page-size="detail.pageSize"
    :total="detail.total"
    :page-sizes="[5, 10]"
    :small="small"
    :background="background"
    layout="total, sizes, prev, pager, next, jumper"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
  ></el-pagination>
  <el-dialog v-model="dialogFormVisible2" title="显示图表">
    <Plantecharts :list="this.list2"></Plantecharts>
  </el-dialog>

</template>